package com.atguigu.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        String brandId = (String) params.get("brandId");
        String catelogId = (String) params.get("catelogId");
        Long min = Long.parseLong((String) params.get("min"));
        Long max = Long.parseLong((String) params.get("max"));;
        if(!StringUtils.isEmpty(key)){
            wrapper.and(w->{
                w.eq("spu_name",key).or().like("spu_name",key);
            });
        }
        if(!StringUtils.isEmpty(brandId)){
            if(Long.parseLong(brandId)>0){
                wrapper.and(w->{
                    w.eq("brand_id",brandId);
                });
            }
        }
        if(!StringUtils.isEmpty(catelogId)){
            if(Long.parseLong(catelogId)>0){
                wrapper.and(w->{
                    w.eq("catalog_id",catelogId);
                });
            }

        }
        if(max>0&&min>0&&max>=min){
            wrapper.and(w->{
                w.between("price",min,max);
            });
        }

        IPage iPage = this.page(new Query<SkuInfoEntity>().getPage(params), wrapper);
        return new PageUtils(iPage);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        return this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
    }

}